package xkp.lesson6;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        //1、根据IP地址+端口号连接服务器
        //注意：127.0.0.1 为本地回环地址 可以测试本机的TCP/IP协议栈是否OK
        Socket socket = new Socket("192.168.93.60",8888);
        //2、数据传递 已经通过Socket建立连接   数据传输    I/O流
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        //3、只要想socket中写数据就是在向远端发送数据
        String str = "hello";
        dataOutputStream.writeUTF(str);
        dataOutputStream.flush();
        DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
        String redServer = dataInputStream.readUTF();
        System.out.println(redServer);
        dataOutputStream.close();
        dataInputStream.close();
        //outputStream.write(str.getBytes());
        //4、只要从socket中读数据，接收数据

        socket.close();
    }
}
